package com.zhoug.common3.crash;


import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import com.zhoug.logger3.Logger;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.zhoug.common3.R;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.lifecycle.Lifecycle;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

/**
 * 异常列表
 *
 * @Author 35574
 * @Date 2020/6/18
 * @Description
 */
public class CrashListActivity extends AppCompatActivity {
    private static final String TAG = ">>>CrashListActivity";
    private ListView mListView;
    private SwipeRefreshLayout mSwipeRefreshLayout;
    private CrashAdapter mAdapter;
    private List<String> mData = new ArrayList<>();
    private TextView mTvCreateCrash;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.common_activity_crash_list);
        findViews();
        initList();
        loadData();
    }

    private void findViews() {
        mListView = findViewById(R.id.listView);
        mSwipeRefreshLayout = findViewById(R.id.swipeRefreshLayout);
        mTvCreateCrash = findViewById(R.id.tv_createCrash);
    }

    private void initList() {
        mTvCreateCrash.setOnClickListener(v -> {
            for (int i = 0; i < 10; i++) {
                new Thread(() -> {
                    if (3 / 0 == 0) {

                    }
                }).start();
            }
        });
        mSwipeRefreshLayout.setColorSchemeColors(ContextCompat.getColor(this, R.color.colorPrimary));
        mSwipeRefreshLayout.setDistanceToTriggerSync(200);
        mSwipeRefreshLayout.setProgressViewEndTarget(true, 200);
        mSwipeRefreshLayout.setProgressBackgroundColorSchemeColor(Color.WHITE);
        mSwipeRefreshLayout.setOnRefreshListener(this::loadData);
        mAdapter = new CrashAdapter(mData);
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener((parent, view, position, id) -> {
            //进入日志详情页面
            if (mData != null && mData.size() > 0) {
                String path = mData.get(position);
                Intent intent = new Intent(CrashListActivity.this, CrashDetailActivity.class);
                Bundle args = new Bundle();
                args.putString("PATH", path);
                intent.putExtras(args);
                startActivity(intent);

            }
        });

        mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                mData.remove(mData.get(position));
                mAdapter.notifyDataSetChanged();
                Toast.makeText(getApplicationContext(), "删除第"+position+"项", Toast.LENGTH_SHORT).show();
                return true;
            }
        });
    }

    /**
     * 获取异常日志列表
     */
    private void loadData() {
        show();
        new Thread(() -> {
            final List<String> allExceptionFiles = BaseUncaughtExceptionHandler.getInstance().getAllExceptionFiles();
            if (getLifecycle().getCurrentState() != Lifecycle.State.DESTROYED) {
                runOnUiThread(() -> onLoadCallback(allExceptionFiles));
            } else {
                Logger.e(TAG, "loadData:页面已销毁");
            }
        }

        ).start();
    }

    private void onLoadCallback(List<String> allExceptionFiles) {
        cancel();
        mData.clear();
        if (allExceptionFiles != null && allExceptionFiles.size() > 0) {
            mData.addAll(allExceptionFiles);
        } else {
            Toast.makeText(this.getApplication(), "没有数据", Toast.LENGTH_SHORT).show();
        }
        mAdapter.notifyDataSetChanged();

    }

    public void show() {
        if (mSwipeRefreshLayout != null && !mSwipeRefreshLayout.isRefreshing()) {
            mSwipeRefreshLayout.setRefreshing(true);
        }
    }

    public void cancel() {
        if (mSwipeRefreshLayout != null && mSwipeRefreshLayout.isRefreshing()) {
            mSwipeRefreshLayout.setRefreshing(false);
        }
    }
}
